package com.wt.admin.config.security;

import cn.hutool.core.util.StrUtil;
import cn.hutool.jwt.JWT;
import com.aizuda.easy.security.code.BasicCode;
import com.aizuda.easy.security.exp.impl.BasicException;
import com.aizuda.easy.security.handler.RepFunctionHandler;
import com.aizuda.easy.security.handler.ReqFunctionHandler;
import com.aizuda.easy.security.util.LocalUtil;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class RenewalHandler implements ReqFunctionHandler, RepFunctionHandler {

    @Override
    public String exec(HttpServletRequest request, String json) {
        String token = request.getHeader("token");
        if(StrUtil.isBlank(token) || LocalUtil.getLocalEntity().getSpecial()){
            return json;
        }
        JWT jwt = JWT.of(token);
        // 判断token 是否过期
        long exp = Long.parseLong(jwt.getPayload("exp").toString());
        if(System.currentTimeMillis() > exp){
            throw new BasicException(BasicCode.BASIC_CODE_99985);
        }
        return json;
    }


    @Override
    public Integer getIndex() {
        return 10;
    }

    @Override
    public String exec(HttpServletResponse response, String json) throws BasicException, IOException {
        return json;
    }


}
